function [ e_Y_FR, m_e_f, cov_e_f, check_f,...
    e_Y_FR_doubledef, m_e_f_doubledef, cov_e_f_doubledef, check_f_doubledef,...
     e_Y_FR_deflator, m_e_f_deflator, cov_e_f_deflator, check_f_deflator]...
     = elasticity_decomp_firm_fun(delta,france,RGDP_cpi_hat_fj,RGDP_cpi_hat_n,...
        VA_fj_0,RGDP_def_hat_fj,RGDP_def_hat_n,RGDP_doubledef_hat_fj,RGDP_doubledef_hat_n)

global shockT alphaT rhoT lambdaT etaT rho sigma lambda eta varphi ...
    tol maxit D_hat_n Xi_hat_mnjf Xi_hat_mnj a_hat_f a_hat ...
    N J FI_J france countrysecD firmsecD_sorted start start_sorted ...
    sum_by_country_dummy ISO sector_algorithm ... 
    sL_n sPi_n sD_n alpha_WIOD_j alpha_WIOD_francej labour_share_PWT
 
 global alphaT rhoT lambdaT etaT shockT rho sigma lambda eta ...
    tol maxit Pi_hat_n D_hat_n Xi_hat_mnjf Xi_hat_mnj a_hat_f a_hat ...
    varphi sL_n sPi_n sD_n N J FI_J france oneminus_alpha_vector ...
    labour_share_PWT countrysecD firmsecD_sorted start start_sorted ...
    sum_by_country_dummy ISO alpha_WIOD_j alpha_WIOD_francej sector_algorithm 

% Calculating the elasticities from the formula on page 1 of master notes 2

%% Using CPI

e_f = (RGDP_cpi_hat_fj-1)/delta;
e_Y_FR = (RGDP_cpi_hat_n(france)-1)/delta;

m_e_f = nanmean(e_f);

w_f = VA_fj_0/sum(VA_fj_0);
w_f_bar = size(w_f,1) - 1;  % N - 1 correction for small small as in COV
C = nancov(w_f*w_f_bar,e_f);
cov_e_f = C(1,2);
clear C;

check_f = e_Y_FR-m_e_f-cov_e_f;




%% Using GDP Deflator

e_f_deflator = (RGDP_def_hat_fj-1)/delta;
e_Y_FR_deflator = (RGDP_def_hat_n(france)-1)/delta;

m_e_f_deflator = nanmean(e_f_deflator);

w_f = VA_fj_0/sum(VA_fj_0);
w_f_bar = size(w_f,1) - 1;  % N - 1 correction for small small as in COV
C_deflator = nancov(w_f*w_f_bar,e_f_deflator);
cov_e_f_deflator = C_deflator(1,2);
clear C_deflator;

check_f_deflator = e_Y_FR_deflator-m_e_f_deflator-cov_e_f_deflator;

%% Using double-GDP Deflator

e_f_doubledef = (RGDP_doubledef_hat_fj-1)/delta;
e_Y_FR_doubledef = (RGDP_doubledef_hat_n(france)-1)/delta;

m_e_f_doubledef = nanmean(e_f_doubledef);

w_f = VA_fj_0/sum(VA_fj_0);
w_f_bar = size(w_f,1) - 1;  % N - 1 correction for small small as in COV
C_doubledef = nancov(w_f*w_f_bar,e_f_doubledef);
cov_e_f_doubledef = C_doubledef(1,2);
clear C_doubledef;

check_f_doubledef = e_Y_FR_doubledef-m_e_f_doubledef-cov_e_f_doubledef;

end

